<template>
  <div class="app-container">
    <el-form :inline="true" class="form-inline">
      <el-form-item>
        <el-button type="success" plain @click="addMsg">新增</el-button>
      </el-form-item>
    </el-form>
    <div class="content">
      <div class="_list">
        <el-table highlight-current-row @selection-change="handleSelectionChange" :height="$tools.tableHeight()" :data="dataList" style="width: 100%" v-loading.body="listLoading">
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column label="消息标题">
            <template slot-scope="scope">
              {{ scope.row.title }}
            </template>
          </el-table-column>
          <el-table-column label="添加时间" width="400">
            <template slot-scope="scope">
              {{ scope.row.createTime | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="scope">
              <el-button type="text" @click.native="ToView(scope.row)">查看</el-button>
              <el-button type="text" @click.native="edit(scope.row)">编辑</el-button>
              <el-button type="text" @click="arrDelete('1', scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pages">
        <div class="left">
          <el-tooltip class="item" effect="dark" content="批量删除" placement="top">
            <el-button size="small" type="danger" @click="arrDelete('2')">批量删除</el-button>
          </el-tooltip>
        </div>
        <div class="right" v-if="total > 0">
          <el-pagination background :total="total" :page-size="15" :current-page.sync="page" layout="total, prev, pager, next, jumper" @current-change="handleCurrentChange">
          </el-pagination>
        </div>
      </div>
    </div>
    <el-dialog title="消息内容" width="40%" :visible.sync="outerVisible">
      <div class="_content" v-html="content"></div>
    </el-dialog>
    <el-dialog title="新增官方消息" width="1090px" :visible.sync="msgVisible">
      <el-form :model="articleInfo" :rules="rules" ref="articleInfo" label-width="80px">
        <el-form-item label="消息标题" prop="title">
          <el-input placeholder="请输入消息标题" style="width:600px;" v-model="articleInfo.title"></el-input>
        </el-form-item>
        <el-form-item label="消息内容" prop="content">
          <ueditor ref="editor" v-model="articleInfo.content" :config="config"></ueditor>
          <!-- <mavon-editor ref="mavonEditor" v-model="articleInfo.markdownContent" :ishljs="true" @imgDel="imgDel" @imgAdd="imgAdd" @change="markdownChange" v-else-if="SYS.setup.edit_type ==="2"" /> -->
        </el-form-item>
        <!-- <el-form-item label="是否推送">
          <el-radio-group v-model="radioGroup">
            <el-radio label="0">不推送</el-radio>
            <el-radio label="1">@所有人</el-radio>
            <el-radio label="2">指定用户</el-radio>
          </el-radio-group>
          <el-autocomplete style="margin-left:20px;" v-if="radioGroup === '2'" v-model="radioState" :fetch-suggestions="querySearchAsync" placeholder="请输入指定的用户" @select="handleSelect"></el-autocomplete>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="msgVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import mixinsTable from '@/util/mixins.table';
import ueditor from '@/components/Editor';

export default {
  name: 'Message',
  mixins: [mixinsTable],
  data () {
    return {
      tabIndex: 1,
      APISERVER: 'officalNews',
      DELETEAPI: 'deleteOfficalNews',
      outerVisible: false,
      msgVisible: false,
      switchValue: false,
      articleInfo: {
        title: '',
        content: ''
      },
      radioState: '',
      radioGroup: '0',
      content: '',
      rowId: '',
      config: {
        initialFrameWidth: 970,
        initialFrameHeight: 420
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在 10 到 52 个字符', trigger: 'blur' }
        ],
        content: [{ required: true, message: '请输入文章详情', trigger: 'change' }]
      }
    };
  },
  components: {
    ueditor
  },
  created () {
    this.getList();
  },
  methods: {
    // 查看
    ToView (item) {
      this.content = item.content;
      this.outerVisible = true;
    },
    addMsg () {
      this.rowId = '';
      this.msgVisible = true;
      this.articleInfo = {
        title: '',
        content: ''
      };
    },
    edit (row) {
      this.rowId = row.id;
      this.msgVisible = true;
      this.articleInfo.title = row.title;
      setTimeout(() => {
        this.articleInfo.content = row.content;
      }, 200);
    },
    querySearchAsync (queryString, cb) {
      var restaurants = this.restaurants;
      var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;

      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        cb(results);
      }, 3000 * Math.random());
    },
    createStateFilter (queryString) {
      return state => {
        return state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0;
      };
    },
    handleSelect (item) {
      console.log(item);
    },
    handleSetTabIndex (data) {
      console.log(data);
      this.tabIndex === '2' && this.$router.push({ path: '/message/feedback' });
    },
    onSubmit () {
      this.$refs.articleInfo.validate(async (valid) => {
        if (valid) {
          let result = '';

          if (this.rowId) {
            result = await this.$API.modOfficalNews(this.rowId, {
              title: this.articleInfo.title,
              content: this.articleInfo.content
            });
          } else {
            result = await this.$API.addOfficalNews({
              title: this.articleInfo.title,
              content: this.articleInfo.content
            });
          }

          if (result.code === 200) {
            this.msgVisible = false;
            this.$message.success('保存成功!');
            this.getList();
          }
        }
      });
    }
  }
};
</script>